package com.lhhu.demo.service.impl;

import com.lhhu.demo.bean.OrderMessage;
import com.lhhu.demo.mapper.OrderMapper;
import com.lhhu.demo.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Auther 虎
 * @Data 2021/3/23 17:34
 * @Version 1.0
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public String addOneOrder(OrderMessage orderMessage) {
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmss");
        SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int random = (int)(Math.random()*10);
        String orderId ='3'+ dateFormat.format(date) + random;
        String orderTime = dateFormat2.format(date);
        orderMessage.setOrderId(orderId);
        orderMessage.setOrderTime(orderTime);
        if(orderMapper.addOneOrder(orderMessage)==1){
            return orderId;
        }
        return "0";
    }

    @Override
    public List<OrderMessage> getAllOrderByStatus(String openid,String orderStatus) {
        if(openid.length()>3){
            if(orderStatus.equals("全部"))return orderMapper.getAllOrderByUser(openid);
            else return orderMapper.getAllOrderByUserAndStatus(openid,orderStatus);
        }
        else return orderMapper.getAllOrderByStatus(orderStatus);
    }

    @Override
    public int confirmOrder(String orderId,String orderStatus) {
        return orderMapper.confirmOrder(orderId,orderStatus);
    }

    @Override
    public List<OrderMessage> getAllOrderByOrderId(String orderId,String orderStatus) {
        return orderMapper.getAllOrderByOrderId(orderId,orderStatus);
    }

    @Override
    public List<OrderMessage> getAllOrderByPhone(String orderPhone,String orderStatus) {
        return orderMapper.getAllOrderByPhone(orderPhone,orderStatus);
    }

    @Override
    public List<OrderMessage> getAllOrderByTime(String begindate, String enddate,String orderStatus) {
        return orderMapper.getAllOrderByTime(begindate, enddate,orderStatus);
    }

    @Override
    public int deleteOneOrder(String orderId) {
        return orderMapper.deleteOneOrder(orderId);
    }
}
